from langchain.memory import ConversationBufferWindowMemory

# 这里设置K值为2，为2记忆两轮对话
from langchain_community.llms.tongyi import Tongyi
import os
os.environ["DASHSCOPE_API_KEY"] = "sk-9d8f1914800e497f8717144e860f99bc"

memory = ConversationBufferWindowMemory(k=2)
memory.save_context({"input": "你好"}, {"output": "什么事？"})
memory.save_context({"input": "1+2等于几"}, {"output": "3"})
res = memory.load_memory_variables({})
print(res)


from langchain.chains import ConversationChain

conversation_with_summary = ConversationChain(
    llm=Tongyi(),
    memory=memory,
    verbose=True
)

res = conversation_with_summary.invoke("阿里巴巴是做什么的")
print(res)
